//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Workflow.ComponentModel;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Web Data Reader")]
    [Description("Given a URL this method downloads file from that location.")]
    [WorkflowCategory("Web")]    
    public class DownloadWebDataSync : Activity
    {
        #region Dependency Properties

        public static DependencyProperty URLProperty = DependencyProperty.Register("URL",
            typeof(string), typeof(DownloadWebDataSync));

        [RequiredInputParam]
        [Name("URL")]
        [Description("URL for downloading the file.")]
        public string URL
        {
            get
            {
                return ((string)(base.GetValue(DownloadWebDataSync.URLProperty)));
            }
            set
            {
                base.SetValue(DownloadWebDataSync.URLProperty, value);
            }
        }

        public static DependencyProperty DownloadFolderProperty = DependencyProperty.Register("DownloadFolder",
            typeof(DirectoryInfo), typeof(DownloadWebDataSync));

        [RequiredInputParam]
        [Name("Directory")]
        [Description("Folder where the file is to be downloaded.")]
        public DirectoryInfo DownloadFolder
        {
            get
            {
                return ((DirectoryInfo)(base.GetValue(DownloadWebDataSync.DownloadFolderProperty)));
            }
            set
            {
                base.SetValue(DownloadWebDataSync.DownloadFolderProperty, value);
            }
        }

        public static DependencyProperty OutFileNameProperty = DependencyProperty.Register("OutFileName",
            typeof(string), typeof(DownloadWebDataSync));

        [RequiredInputParam]
        [Name("Downloaded File Name")]
        [Description("URL for downloading the file.")]
        public string OutFileName
        {
            get
            {
                return ((string)(base.GetValue(DownloadWebDataSync.OutFileNameProperty)));
            }
            set
            {
                base.SetValue(DownloadWebDataSync.OutFileNameProperty, value);
            }
        }

        #endregion

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Uri query = new Uri(this.URL);
            System.Net.WebRequest webReq = HttpWebRequest.Create(query);
            WebResponse webResp = webReq.GetResponse();

            System.IO.Stream stream = webResp.GetResponseStream();
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);

            using (System.IO.FileStream fileWriter = new System.IO.FileStream(this.OutFileName,
                System.IO.FileMode.OpenOrCreate))
            {
                using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fileWriter))
                {

                    byte[] buffer = new byte[100];
                    while (true)
                    {
                        int read = br.Read(buffer, 0, 100);
                        bw.Write(buffer);
                        if (read < 100)
                            break;
                    }
                }
            }

            webResp.Close();

            return ActivityExecutionStatus.Closed;
        }
    }
}
